<template>
  <div class="container">
    <div class="app-container">
      <!-- 添加权限按钮 -->
      <el-button
        type="primary"
        size="mini"
        class="add-btn"
        @click="showDialog(1, '0')"
        >添加权限</el-button
      >
      <!-- 表格 -->
      <!-- 树形表格的渲染： -->
      <!-- 1. 给它树形结构的数据 -->
      <!-- 2. 在el-table上指定一个唯一标识：row-key="id" -->
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="text"
              size="mini"
              v-if="row.type === 1"
              @click="showDialog(2, row.id)"
              >添加</el-button
            >
            <el-button type="text" size="mini" @click="showEditDialog(row.id)"
              >编辑</el-button
            >
            <el-button type="text" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加/编辑弹窗 -->
    <el-dialog title="添加权限" :visible.sync="visible" @close="dialogClose">
      <!-- 表单 -->
      <el-form
        ref="permissionRef"
        label-width="80px"
        :model="permissionForm"
        :rules="permissionRules"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="permissionForm.description"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="permissionForm.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- 按钮 -->
      <div style="margin-top: 40px; text-align: center">
        <el-button type="primary" size="mini" @click="btnOK">确定</el-button>
        <el-button size="mini" @click="visible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addPermission,
  editPermission,
  getPermissionDetail,
  getPermissionList,
} from "@/api/permission";
import { listToTree } from "@/utils";
export default {
  name: "Permission",
  data() {
    return {
      list: [],
      // 控制弹窗显示与隐藏
      visible: false,
      // 表单数据对象
      permissionForm: {
        name: "",
        code: "",
        description: "",
        enVisible: "0",
        type: "",
        pid: "",
      },
      // 表单验证规则对象
      permissionRules: {
        name: [
          { required: true, message: "权限名称不能为空", trigger: "blur" },
        ],
        code: [
          { required: true, message: "权限标识不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    // 获取权限列表数据
    async getPermissionList() {
      const res = await getPermissionList();
      // console.log(listToTree(res.data, 0));
      this.list = listToTree(res.data, 0);
    },
    // 点击添加权限按钮
    showDialog(type, pid) {
      this.permissionForm.type = type;
      this.permissionForm.pid = pid;
      // 显示弹窗
      this.visible = true;
    },
    // 点击编辑按钮
    async showEditDialog(id) {
      // 显示弹窗
      this.visible = true;
      // 调用获取权限详情数据接口
      const res = await getPermissionDetail(id);
      this.permissionForm = res.data;
    },
    // 点击X和蒙板时触发
    dialogClose() {
      // 清空表单内容和校验结果
      this.$refs.permissionRef.resetFields();
      // 手动清空数据
      this.permissionForm = {
        name: "",
        code: "",
        description: "",
        enVisible: "0",
        type: "",
        pid: "",
      };
    },
    // 点击了弹窗中的确定按钮
    btnOK() {
      // 兜底校验
      this.$refs.permissionRef.validate(async (isOK) => {
        if (isOK) {
          // 校验通过
          if (this.permissionForm.id) {
            // 编辑
            // 调用编辑权限接口
            await editPermission(this.permissionForm);
            // 提示用户
            this.$message.success("编辑成功");
          } else {
            // 添加
            // 调用添加权限接口
            await addPermission(this.permissionForm);
            // 提示用户
            this.$message.success("添加成功");
          }
          // 关闭弹窗
          this.visible = false;
          // 刷新列表数据
          this.getPermissionList();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.add-btn {
  margin: 20px 10px;
}
</style>
